/**
 * Created by john on 2018/1/12.
 */
$(function () {
    //变量声明
    var page = 1
    var model_id = ''
    var leixinid = ''
    var yeshu = 0
    //标识符
    var loading = false;

    //获取头部

        $.ajax({
            url: connect + 'index/articleColumn',
            type: 'post',
            data: {'mstr': mstr, name: 'notice'},
            success: function (data) {

                var html = template('newsHead', data)

                var html1 = template('newsTab_bot', data)

                var html2 = template('Popup_bot_model', data)

                $('#Popup_bot').append(html2)
                $('#pagenavi1 > ul').html(html)
                $('#slider1 > ul').append(html1)

                toggleTab()

                model_id = $('#pagenavi1 > ul li').eq(0).attr('modelId')
                leixinid = $('#pagenavi1 > ul li').eq(0).attr('leixinId')

                //进入后触发函数
                lanmu_list(model_id, leixinid, yeshu)
            },
        })



    //头部搜索
    $('.weui-search-bar__form').on('submit', function (e) {
        var content = $('#searchInput').val()
        $(e.target).parents(".weui-search-bar").removeClass("weui-search-bar_focusing").find(".weui-search-bar__input").val("").blur();

        $.ajax({
            url: connect + 'index/columnContent',
            data: {mstr: mstr, column_id: leixinid, model_id: model_id, page: 1, pagesize: 10, seek: content},
            type: 'post',
            success: function (data) {
                if (!data.errcode == 0 && page == 1) {
                    $('#slider1 > ul li').eq(yeshu).html(
                        '<div class="weui-loadmore weui-loadmore_line " style="border: 0px;line-height: inherit">' +
                        '<div class="iconfont icon-wuxiaoxijilu" style="font-size: 50px;color:#999999"></div>' +
                        '<span class="weui-loadmore__tips" style="padding: 1em .55em"> '+data.info+' </span>' +
                        '</div>')
                    return
                }

                if (data.errcode !== 0 && page !== 1) {
                    $('#slider1 > ul li').eq(yeshu).find('.weui-loadmore').remove()
                    $('#slider1 > ul li').eq(yeshu).append(
                        '<div class="weui-loadmore weui-loadmore_line">' +
                        '<span class="weui-loadmore__tips">无更多数据</span>' +
                        '</div>'
                    )
                    return
                }

                data.connect = connectImg

                $.each(data.data, function (i, e) {
                    e.update_time = getTime(e.update_time)
                })

                var html = template('newsCen_list', data)

                $('#slider1 > ul li').eq(yeshu).html(html)

                if (data.data.length < 10) {
                    $('#slider1 > ul li').eq(yeshu).find('.weui-loadmore').remove()
                    $('#slider1 > ul li').eq(yeshu).append(
                        '<div class="weui-loadmore weui-loadmore_line">' +
                        '<span class="weui-loadmore__tips">无更多数据</span>' +
                        '</div>'
                    )
                }

                $('#slider1 > ul').css({height: $('#slider1 > ul li').eq(yeshu).css('height'), overflow: 'hidden'})
            }
        })
    })

    //弹出框
    $('#searchBar .zhanshi').click(function (e) {
        e.stopPropagation();

        if ($("div.con").hasClass("hide")) {
            $("div.con").removeClass("hide");
            $('div.con .popup_content').stop().animate({height: 150}, 300)
            $('#searchBar .zhanshi').css({"backgroundImage": "url('image/brand_after.png')"})
        } else {
            $('div.con .popup_content').stop().animate({height: 0}, 300, function () {
                $("div.con").addClass("hide");
            })
            $('#searchBar .zhanshi').css({"backgroundImage": "url('image/brand.png')"})

        }

        $(document).click(function () {
            if (!$("div.con").hasClass("hide")) {
                $('div.con .popup_content').stop().animate({height: 0}, 300, function () {
                    $("div.con").addClass("hide");
                })
                $('#searchBar .zhanshi').css({"backgroundImage": "url('image/brand.png')"})
            }
        })
    });

    //滚动加载
    $(document.body).infinite(200).on("infinite", function () {
        console.log(loading);
        if (loading) return;
        loading = true;
        console.log('触发了');
        page++
        lanmu_list(model_id, leixinid, yeshu)
        //延迟加载
    });

    //生成新闻列表函数
    function lanmu_list(model, colum, index) {
        var _index = index
        $.ajax({
            url: connect + 'index/columnContent',
            type: 'post',
            data: {mstr: mstr, column_id: colum, model_id: model, page: page, pagesize: 10},
            success: function (data) {
                console.log(data)
                console.log(page);
                if (!data.errcode == 0 && page == 1) {
                    $('#slider1 > ul li').eq(_index).html(
                        '<div class="weui-loadmore weui-loadmore_line " style="border: 0px;line-height: inherit">' +
                        '<div class="iconfont icon-wuxiaoxijilu" style="font-size: 50px;color:#999999"></div>' +
                        '<span class="weui-loadmore__tips" style="padding: 1em .55em"> '+data.info+' </span>' +
                        '</div>')
                    return
                }

                if (data.errcode !== 0 && page !== 1) {
                    $('#slider1 > ul li').eq(_index).find('.weui-loadmore').remove()
                    $('#slider1 > ul li').eq(_index).append(
                        '<div class="weui-loadmore weui-loadmore_line">' +
                        '<span class="weui-loadmore__tips">无更多数据</span>' +
                        '</div>'
                    )
                    return
                }

                data.connect = connectImg

                $.each(data.data, function (i, e) {
                    e.update_time = getTime(e.update_time)
                })

                var html = template('newsCen_list', data)

                if (page == 1) {
                    $('#slider1 > ul li').eq(_index).html(html)
                    loading = false;
                } else {
                    $('#slider1 > ul li').eq(_index).find('.weui-loadmore').remove()
                    $('#slider1 > ul li').eq(_index).append(html)
                    loading = false;
                }

                if (data.data.length < 10) {
                    $('#slider1 > ul li').eq(_index).find('.weui-loadmore').remove()
                    $('#slider1 > ul li').eq(_index).append(
                        '<div class="weui-loadmore weui-loadmore_line">' +
                        '<span class="weui-loadmore__tips">无更多数据</span>' +
                        '</div>'
                    )
                }

                $('#slider1 > ul').css({height: $('#slider1 > ul li').eq(index).css('height'), overflow: 'hidden'})

            }
        })
    }

    //tab切换函数
    function toggleTab() {
        $(".find_nav_list").css("left", 0);

        $(".find_nav_list li").each(function () {
            $(".sideline").css({left: 0});
            $(".find_nav_list li").eq(0).addClass("find_nav_cur").siblings().removeClass("find_nav_cur");
        });
        var nav_w = $(".find_nav_list li").first().width();
        $(".sideline").width(nav_w);

        $(".find_nav_list li").on('click', function () {
            nav_w = $(this).width();
            $(".sideline").stop(true);
            $(".sideline").animate({left: $(this).position().left}, 300);
            $(".sideline").animate({width: nav_w});
            $(this).addClass("find_nav_cur").siblings().removeClass("find_nav_cur");
            if($('.find_nav_list').width()> $('.find_nav').width()){
                var fn_w = ($(".find_nav").width() - nav_w) /2;
            }else{
                var fn_w = ($(".find_nav").width() - nav_w) ;
            }

            var fnl_l;

            var fnl_x = parseInt($(this).position().left);

            if (fnl_x  <= fn_w) {
                fnl_l = 0;
            } else if (fn_w - fnl_x <= flb_w - fl_w) {
                fnl_l = flb_w - fl_w;
            } else {
                fnl_l = fn_w - fnl_x;
            }

            $(".find_nav_list").stop().animate({
                "left": fnl_l
            }, 300);

        });
        var fl_w = $(".find_nav_list").width();
        var flb_w = $(".find_nav_left").width();
        $(".find_nav_list").on('touchstart', function (e) {
            var touch1 = e.originalEvent.targetTouches[0];
            x1 = touch1.pageX;
            y1 = touch1.pageY;
            ty_left = parseInt($(this).css("left"));
        });
        $(".find_nav_list").on('touchmove', function (e) {
            var touch2 = e.originalEvent.targetTouches[0];
            var x2 = touch2.pageX;
            var y2 = touch2.pageY;
            if (ty_left + x2 - x1 >= 0) {
                $(this).css("left", 0);
            } else if (ty_left + x2 - x1 <= flb_w - fl_w) {
                $(this).css("left", flb_w - fl_w);
            } else {
                $(this).css("left", ty_left + x2 - x1);
            }
            if (Math.abs(y2 - y1) > 0) {
                e.preventDefault();
            }

        });

        var page1 = 'pagenavi' + 1;

        var as = document.getElementById('pagenavi1').getElementsByTagName('a');
        var tt = new TouchSlider({
            id: 'slider1', 'auto': '-1', fx: 'ease-out', direction: 'left', speed: 600, timeout: 5000,
            'before': function (index) {


                var as = document.getElementById(this.page).getElementsByTagName('a');
                as[this.p].className = '';
                if (index >= as.length) {
                    return
                }
                this.p = index;

                var fnl_x = parseInt($(".find_nav_list li").eq(this.p).position().left);

                nav_w = $(".find_nav_list li").eq(this.p).width();
                $(".sideline").stop(true);
                $(".sideline").animate({left: $(".find_nav_list li").eq(this.p).position().left}, 300);
                $(".sideline").animate({width: nav_w}, 100);
                $(".find_nav_list li").eq(this.p).addClass("find_nav_cur").siblings().removeClass("find_nav_cur");

                if($('.find_nav_list').width()> $('.find_nav').width()){
                    var fn_w = ($(".find_nav").width() - nav_w) /2;
                }else{
                    var fn_w = ($(".find_nav").width() - nav_w) ;
                }
                var fnl_l;
                if (fnl_x  <= fn_w) {
                    fnl_l = 0;
                } else if (fn_w - fnl_x  <= flb_w - fl_w) {
                    fnl_l = flb_w - fl_w;
                } else {
                    fnl_l = fn_w - fnl_x;
                }

                $(".find_nav_list").stop().animate({
                    "left": fnl_l
                }, 300);

            },
            'after': function (index) {

                model_id = $('#pagenavi1 > ul li').eq(index).attr('modelId')
                leixinid = $('#pagenavi1 > ul li').eq(index).attr('leixinId') == undefined ? '0' : $('#pagenavi1 > ul li').eq(index).attr('leixinId')
                yeshu = index
                //var leixinid = $('#pagenavi1 > ul li').eq(0).attr('leixinId')
                page = 1
                console.log(page);
                lanmu_list(model_id, leixinid, index)

            }
        });
        tt.page = page1;
        tt.p = 0;
        //console.dir(tt); console.dir(tt.__proto__);
        for (var i = 0; i < as.length; i++) {
            (function () {
                var j = i;
                as[j].tt = tt;
                as[j].onclick = function (e) {
                    this.tt.slide(j);
                    //console.log(e.target.innerHTML) ?????????
                    return false;
                }
            })();
        }

    }
})